/**
 * 
 */
package edu.pku.sei.modeler.editparts;

import java.beans.PropertyChangeEvent;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.draw2d.IFigure;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.UnexecutableCommand;
import org.eclipse.gef.editpolicies.DirectEditPolicy;
import org.eclipse.gef.editpolicies.GraphicalNodeEditPolicy;
import org.eclipse.gef.requests.CreateConnectionRequest;
import org.eclipse.gef.requests.ReconnectRequest;

import edu.pku.sei.modeler.edit.parts.AbstractNodeEditPart;
import edu.pku.sei.modeler.figures.FinalStateFigure;
import edu.pku.sei.modeler.model.core.MetaElement;
import edu.pku.sei.modeler.model.structure.MetaLink;
import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.modeler.model.uml.BehaviorStateMachines.FinalState;
import edu.pku.sei.modeler.model.uml.BehaviorStateMachines.Transition;
import edu.pku.sei.modeler.uml.commands.UMLConnectionCreateCommand;

/**
 * @author YanYan
 *
 */
public class FinalStateEditpart extends AbstractUMLNodeEditPart {

	public FinalStateEditpart(MetaShapeElement model) {
		super(model);
	}
	
	@Override
	protected DirectEditPolicy createDirectEditPolicy() {
		return null;
	}
	
	protected IFigure getDirectEditFigure() {
		return null;
	}

	@Override
	protected IFigure createFigure() {
		return new FinalStateFigure();
	}
	
	protected void createEditPolicies() {
		super.createEditPolicies();
		
		installEditPolicy(EditPolicy.GRAPHICAL_NODE_ROLE, new GraphicalNodeEditPolicy() {
			protected Command getConnectionCompleteCommand(CreateConnectionRequest request) {
				
				MetaLink newObj = (MetaLink)request.getNewObject();
				MetaElement ele = newObj.getModelElement();
				
				
				
				if (ele instanceof Transition) {
					UMLConnectionCreateCommand cmd = (UMLConnectionCreateCommand) request.getStartCommand();
					cmd.setTarget((MetaNode)getHost().getModel());
					return cmd;
				}
							
				return null;
			}
			protected Command getConnectionCreateCommand(CreateConnectionRequest request) {
				return null;
			}
			protected Command getReconnectSourceCommand(ReconnectRequest request) {
				return UnexecutableCommand.INSTANCE;
			}
			protected Command getReconnectTargetCommand(ReconnectRequest request) {
				return UnexecutableCommand.INSTANCE;
			}
		});
	}

	protected void handlePropertyChanged(PropertyChangeEvent event) {
		String propertyName = event.getPropertyName();
		super.handlePropertyChanged(event);
	}
	
	
	protected void refreshVisuals() {
		super.refreshVisuals();
		MetaNode modelEle = getNode();
		
		if (modelEle.getWidth()!=20 || modelEle.getHeight()!=20) {
			modelEle.setWidth(20);
			modelEle.setHeight(20);
		} 
	}

}
